﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using Splashscreen;
using System.Threading;
using SARD.Aux_Forms;
using System.Diagnostics;



///This program was created by Fabio Miguel de Matos Ravanelli, Rita Corte and Alexandre.
///The inspiration for this program was the mater thesis of Tiago Leão who developed a program to calculate 
///some quality  control image parameters like MTF and NPS.
/// For copyright prupouses the names of the developers of the program will be showed in a separete process (thread) that will be displayed for 3 secs and then closed.
/// During this time the software will do nothing but wait.
namespace SARD.MainForms
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// This section of the code holds two variable that will be used by every mdichild to store data like the position of the selected area by the user.
        /// With this one it will be possible to use the same image coordinates along different imagens (without need of re-selecting the areas).
        /// </summary>
        public List<Rectangle> rect;
        public List<Point> point;


        /// <summary>
        /// Starts the main window showing a splash screen before the program itself.
        /// The program is activated by a double click.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            rect = new List<Rectangle>();
            point = new List<Point>();
            Thread.Sleep(3000);
            Splasher.Close();
        }

        /// <summary>
        /// Starts the main window showing a splash screen before the program itself.
        /// This program recieves a string file as a parameter and shows the proper image.
        /// </summary>
        /// <param name="file">
        /// Recieves an image as a parameter and displays the proper image.
        /// </param>
        public MainForm(string file)
        {
            InitializeComponent();
            rect = new List<Rectangle>();
            point = new List<Point>();
            Thread.Sleep(3000);
            Splasher.Close();


            try
            {
                // Declare the child form as a new one.

                MainForms.DicomImageBox childForm = new MainForms.DicomImageBox(file);
                childForm.Text =file.Substring( file.LastIndexOf("\\"));
                // Set the main form as a parent form.
                childForm.MdiParent = this;
                // Show the child form.
                childForm.Show();


            }
            catch (Exception ex)
            {
                // The user lacks appropriate permissions to read files, discover paths, etc.
                MessageBox.Show("Não foi possível ler um dos arquivos solicitados.\n\n" +
                    "Mensagem de erro: " + ex.Message + "\n\n" + "Para mais detalhes:\n\n" + ex.StackTrace);
            }



        }

        /// <summary>
        /// This function is used to change the mdichild layout (arrange the windows).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Set the layout to cascade.
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void tileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Set the layout to tile horizontal.
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Set the layout to tile vertical.
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void arrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Set the layout to arrange icons.
            this.LayoutMdi(MdiLayout.ArrangeIcons);
        }


        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        //The rest of the code in this file will deal only with the interface and any user interaction with this one.

        /// <summary>
        /// This function allows the user to select one or more image to be openned.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newChildToolStripMenuItem_Click(object sender, EventArgs e)
        {


            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            ///There is no restriction of file type, the user will have the option to see any file in the selected directory.
            ///But only a dicom file is a valid parameter, if it isn't the case an exception will be thrown.
            openFileDialog1.Title = "Selecionar Imagens DICOM";
            openFileDialog1.Filter = "Arquivos DICOM(*.*)|*.*";
            openFileDialog1.Multiselect = true;
            //openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName == "")
                return;

            string[] safename = openFileDialog1.SafeFileNames;
            int i = 0;
            foreach (String file in openFileDialog1.FileNames)
            {
                try
                {
                    // Declare the child form as a new one.

                    MainForms.DicomImageBox childForm = new MainForms.DicomImageBox(file);
                    childForm.Text = safename[i];
                    i++;
                    // Set the main form as a parent form.
                    childForm.MdiParent = this;
                    // Show the child form.
                    childForm.Show();


                }
                catch (Exception ex)
                {
                    // The user lacks appropriate permissions to read files, discover paths, etc.
                    MessageBox.Show("Não foi possível ler um dos arquivos solicitados.\n\n" +
                        "Mensagem de erro: " + ex.Message + "\n\n" + "Para mais detalhes:\n\n" + ex.StackTrace);
                }

            }
        }


        /// <summary>
        /// Opens the database and retrieves the dicom image stored into a specific test.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void visualizarBancoDeDadosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SARD.Aux_Forms.local showoptions = new local();
            showoptions.ShowDialog();

            if (showoptions.getfile() != null)
                openfile(showoptions.getfile());

        }


        /// <summary>
        /// Opens the desired file.
        /// </summary>
        /// <param name="file">
        /// It's the file's path.
        /// </param>
        private void openfile(string file) 
        {
            try
            {
                // Declare the child form as a new one.

                MainForms.DicomImageBox childForm = new MainForms.DicomImageBox(file);
                childForm.Text = file.Substring(file.LastIndexOf("\\"));
                // Set the main form as a parent form.
                childForm.MdiParent = this;
                // Show the child form.
                childForm.Show();


            }
            catch (Exception ex)
            {
                // The user lacks appropriate permissions to read files, discover paths, etc.
                MessageBox.Show("Não foi possível ler um dos arquivos solicitados.\n\n" +
                    "Mensagem de erro: " + ex.Message + "\n\n" + "Para mais detalhes:\n\n" + ex.StackTrace);
            }
        }

    }
}
